package com.open.common.task;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.open.common.utils.R;
import com.open.system.domain.LoanApplyDO;
import com.open.system.domain.LoanApplyLogDO;
import com.open.system.domain.LoanUserDO;
import com.open.system.service.LoanAdminLogService;
import com.open.system.service.LoanApplyLogService;
import com.open.system.service.LoanApplyService;
import com.open.system.service.LoanUserService;

@Component
public class FaceAuthJob implements Job{
	
	@Autowired
	private LoanApplyService loanApplyService;
	
	@Autowired
	private LoanUserService loanUserService;
	
	@Autowired
	private LoanApplyLogService loanApplyLogService;
	
	@Autowired
	private LoanAdminLogService loanAdminLogService;
	
	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		
		System.out.println("已放款”后第二天状态自动生成为“还款中");
		
		//3已放款  4，还款中(已还款在晚上12点自动改成还款中)“已放款”后第二天状态自动生成为“还款中”。
		Map<String,Object> mapApply=new HashMap<String,Object>();
		mapApply.put("status","3");
		List<LoanApplyDO>loanApplys=loanApplyService.list(mapApply);
		if(loanApplys!=null && loanApplys.size()>0){
			for (int i = 0; i < loanApplys.size(); i++) {
				LoanApplyDO applyDO=loanApplys.get(i);
				Map<String,Object> map=new HashMap<String,Object>();
				map.put("userId", applyDO.getUserId());
				LoanApplyLogDO applyLogDO=loanApplyLogService.getLastNewData(map);
				LoanUserDO userDO=loanUserService.get(applyDO.getUserId());
				applyDO.setStatus("4");
				userDO.setStatus("4");
				applyLogDO.setStatus("4");
				loanUserService.update(userDO);
				loanApplyService.update(applyDO);
				loanApplyLogService.update(applyLogDO);
				//--操作日志--开始--
				String shopId="1";
				String adminName="系统自动";
				String Module="系统审批";
				String detail="系统自动通过【"+applyDO.getRealName()+"】【已放款-->还款中】";
				loanAdminLogService.saveLog(shopId, adminName, Module, detail);
				//--操作日志--结束--
			}
		}	
	}

}
